package top.personal.sglmsn.upload.controller;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import top.personal.sglmsn.upload.entity.FileUploadEntity;
import top.personal.sglmsn.upload.service.FileService;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;

/**
 * Upload controller
 *
 * @author 王涛
 */
@Controller
@RequestMapping("upload")
@Slf4j
public class UploadController {

    @Autowired
    FileService fileService;

    @RequestMapping("uploadFile")
    @ResponseBody
    public Object uploadFile(@RequestParam("file") MultipartFile file,String pwd) {
        return fileService.uploadFile(file, pwd);
    }

    @RequestMapping("uploadFiles")
    @ResponseBody
    public Object uploadFiles(@RequestParam("file") MultipartFile[] file,String pwd) {
        return fileService.uploadFile(file, pwd);
    }


    @RequestMapping("{uid}")
    public void getFile(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response,String pwd) throws IOException {
        FileUploadEntity fileUploadEntity = fileService.getFile(uid);
        if (fileUploadEntity == null) {
            response.sendError(HttpStatus.HTTP_NOT_FOUND,"资源异常,请联系管理员");
            return;
        }

        if (fileUploadEntity.getPwdEntity() != null) {
            log.info("私密文件读取{}",pwd);
            String b = DigestUtil.sha256Hex(pwd);
            if (Objects.equals(fileUploadEntity.getPwdEntity().getPwd(), b)==false) {
                log.error("密码异常{}:{}",fileUploadEntity.getPwdEntity().getPwd(), b);
                response.setStatus(HttpStatus.HTTP_UNAUTHORIZED);
                try {
                    response.sendError(HttpServletResponse.SC_UNAUTHORIZED,"权限异常");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return ;
            }
        }


        File file = new File(FileService.parent.getAbsoluteFile(), fileUploadEntity.getFileMd5Entity().getRelative_path());
        ServletOutputStream outputStream = null;
        if (file.exists() == false) {
            response.sendError(HttpStatus.HTTP_NOT_FOUND,"资源异常,请联系管理员");
            return ;
        }


        try {
            response.setContentType("application/"+fileUploadEntity.getExtension_name());
            response.addHeader("Content-Disposition","attachment;filename="+new String(fileUploadEntity.getFile_name().getBytes("UTF-8"),"ISO8859-1"));
            String file_name = fileUploadEntity.getFile_name();
            outputStream = response.getOutputStream();
            Files.copy(file.toPath(), outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
